#include "MagnusEngine.h"

using namespace std;
using namespace me;

MagnusEngine::MagnusEngine(sf::VideoMode vMode, string windowTitle, sf::Uint32 style)
{
    m_window.create(vMode, windowTitle, style);
    active = true;
}

MagnusEngine::~MagnusEngine()
{
    //dtor
}


void MagnusEngine::update()
{
    if (m_window.isOpen())
    {
        sf::Event windowEvent;
        while (m_window.pollEvent(windowEvent))
        {
            if (windowEvent.type == sf::Event::Closed)
                close();

            else if (windowEvent.type == sf::Event::KeyPressed)
                onKey(windowEvent.key.code, true, windowEvent.key.alt, windowEvent.key.control, windowEvent.key.shift, windowEvent.key.system);

            else if (windowEvent.type == sf::Event::KeyReleased)
                onKey(windowEvent.key.code, false, windowEvent.key.alt, windowEvent.key.control, windowEvent.key.shift, windowEvent.key.system);

            else if (windowEvent.type == sf::Event::MouseButtonPressed)
                onMouseClick(windowEvent.mouseButton.button, true, windowEvent.mouseButton.x, windowEvent.mouseButton.y);

            else if (windowEvent.type == sf::Event::MouseButtonReleased)
                onMouseClick(windowEvent.mouseButton.button, false, windowEvent.mouseButton.x, windowEvent.mouseButton.y);

        }
        me::World* cWorld = m_worldManager.getCurrentWorld();
        if (cWorld != NULL)
        {
            cWorld->update();
        }

        onPreRender();

        m_window.clear(sf::Color(0, 0, 0));


        onRender();

        m_window.display();

        onPostRender();
    }
    else
        active = false;
}

void MagnusEngine::close()
{
    onClose();
    m_window.close();
    active = false;
}



void MagnusEngine::onClose(){}
void MagnusEngine::onPreRender(){}
void MagnusEngine::onRender(){}
void MagnusEngine::onPostRender(){}
void MagnusEngine::onKey(sf::Uint32 key, bool pressed, bool alt, bool control, bool shift, bool system){}
void MagnusEngine::onMouseClick(sf::Uint32 button, bool pressed, int x, int y){}

